package linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

public class SinglyLinkedList implements Iterable<Integer> {
    private Node head;

    /**
     * 迭代器遍历
     *
     * @return 匿名内部类
     */
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node pointer = head;

            @Override
            public boolean hasNext() {
                return pointer != null;
            }

            @Override
            public Integer next() {
                int value = pointer.value;
                pointer = pointer.next;
                return value;
            }
        };
    }

    private static class Node {
        int value;
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    //  无元素插入以及有元素头插可以进行合并
    public void addFirst(int value) {
        head = new Node(value, head);
    }

    /**
     * while遍历元素
     *
     * @param consumer 要执行的操作
     */
    public void loop1(Consumer<Integer> consumer) {
        Node pointer = head;
        while (pointer != null) {
            consumer.accept(pointer.value);
            pointer = pointer.next;
        }
    }

    /**
     * for循环遍历元素
     *
     * @param consumer 要执行的操作
     */
    public void loop2(Consumer<Integer> consumer) {
        for (Node pointer = head; pointer != null; pointer = pointer.next) {
            consumer.accept(pointer.value);
        }
    }

    /**
     * 查找最后一个节点
     *
     * @return 返回节点或节点不存在时为空
     */
    public Node findLast() {
        if (head == null) { // 空链表
            return null;
        }
        Node pointer;
        for (pointer = head; pointer.next != null; pointer = pointer.next) {
        }
        return pointer;
    }

    public void addLast(int value) {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
            return;
        }
        last.next = new Node(value, null);
    }

    /**
     * 通过索引获取node值
     *
     * @param index 传入索引
     * @return
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throwIllegalIndex(index);
        }
        return node.value;
    }

    /**
     * 通过索引查询node节点
     *
     * @param index 目标索引
     * @return
     */
    private Node findNode(int index) {
        int i = 0;
        for (Node pointer = head; pointer != null; pointer = pointer.next) {
            if (i == index) {
                return pointer;
            }
            i++;
        }
        return null;
    }

    /**
     * 通过索引插入值
     *
     * @param index 插入索引
     * @param value 插入值
     */
    public void insert(int index, int value) {
        if (index == 0) { // 当索引为0时的情况
            addFirst(value);
            return;
        }
        Node previous = findNode(index - 1);
        if (previous == null) { // 当前面没有节点时的情况
            throwIllegalIndex(index);
        }
        previous.next = new Node(value, previous.next);
    }

    private static void throwIllegalIndex(int index) {
        throw new IllegalArgumentException(String.format("您输入的索引[%d]有误！", index));
    }

    /**
     * 删除第一个节点
     */
    public void removeFirst() {
        if (head == null) {
            throwIllegalIndex(0);
        }
        head = head.next;
    }

    /**
     * 通过索引删除节点
     * @param index
     */
    public void remove(int index) {
        if(index<0){
            throw new IllegalArgumentException("Index must be non-negative");
        }
        if (index == 0) { // 考虑索引为0
            removeFirst();
        }
        Node previous = findNode(index - 1);
        if (previous == null) { // 上一个节点无法找到
            throwIllegalIndex(index);
        }
        if (previous.next == null) { // 删除节点无法找到
            throwIllegalIndex(index);
        }
        previous.next = previous.next.next;
    }
}
